package com.oneinlet.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by WangZiHe on 2017/11/5
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */
//新增spring-data-redis，已经配置好并可以测试启用，不要放在dao层用@Repository 注解，不会生效， 应该放在sevice层
@Service
public class RedisService {



    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void setKeyValue(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value, 60 * 10, TimeUnit.SECONDS);
    }

    public String getValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void delValue(String key) {
        stringRedisTemplate.delete(key);
    }

    public boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    public void addSetValue(String key, ArrayList<String> value) {
        stringRedisTemplate.opsForSet().add(key, value.toArray(new String[value.size()]));
    }

    public boolean setExpire(String key, long time) {
        return stringRedisTemplate.expire(key, time, TimeUnit.MILLISECONDS);
    }

    public boolean haskeyFromList(String key, String element) {
        return stringRedisTemplate.opsForSet().isMember(key, element);
    }

    public Set<String> getSetValues(String key) {
        return stringRedisTemplate.opsForSet().members(key);
    }


}
